package com.example.mywf.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.mywf.bean.CityBean;

import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private DBOpenHelper OpenHelper;

    public DBUtils(Context context) {
        OpenHelper = new DBOpenHelper(context, "mydb", null, 1);
    }

    public void insert(String head, String name, String number, String password, String address, String sex, int age) {
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("head", head);
        values.put("name", name);
        values.put("number", number);
        values.put("password", password);
        values.put("address", address);
        values.put("sex", sex);
        values.put("age", age);
        db.insert("tb_friend", null, values);
        db.close();
    }

    //查询所有城市记录
    public List<CityBean> select() {
        List<CityBean> list = new ArrayList<CityBean>();
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        String sql = "select * from tb_city";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            CityBean cityBean = new CityBean();
            cityBean.setCityId(cursor.getString(0));
            cityBean.setCityName(cursor.getString(1));
            cityBean.setCitypinyin(cursor.getString(2));
            list.add(cityBean);
        }
        cursor.close();
        db.close();
        return list;
    }

    //通过拼音查询城市记录
    public List<CityBean> select(String pinyin) {
        List<CityBean> list = new ArrayList<CityBean>();
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        String sql = "select * from tb_city where pinyin like '%" + pinyin + "%'";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            CityBean cityBean = new CityBean();
            cityBean.setCityId(cursor.getString(0));
            cityBean.setCityName(cursor.getString(1));
            cityBean.setCitypinyin(cursor.getString(2));
            list.add(cityBean);
        }
        cursor.close();
        db.close();
        return list;
    }

    //通过拼音查询城市记录
    public List<CityBean> selectmycity() {
        List<CityBean> list = new ArrayList<CityBean>();
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        String sql = "select * from tb_city where mark = 1";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            CityBean cityBean = new CityBean();
            cityBean.setCityId(cursor.getString(0));
            cityBean.setCityName(cursor.getString(1));
            cityBean.setCitypinyin(cursor.getString(2));
            list.add(cityBean);
        }
        cursor.close();
        db.close();
        return list;
    }

    public void savemycity(String cityid) {
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        String sql = "update tb_city set mark = 1 where id = '" + cityid + "'";
        db.execSQL(sql);
        db.close();
    }

    public void deletemycity(String cityid) {
        SQLiteDatabase db = OpenHelper.getWritableDatabase();
        String sql = "update tb_city set mark = 0 where id = '" + cityid + "'";
        db.execSQL(sql);
        db.close();
    }

}
